//
//  HREmotionManager.swift
//  weiBo
//
//  Created by helong on 16/7/20.
//  Copyright © 2016年 helong. All rights reserved.
//
/// 管理表情的单例
import UIKit

class HREmotionManager: NSObject {
    //  单例对象
    static let manager: HREmotionManager = HREmotionManager()
    //  私有化构造函数
    private override init() {
        super.init()
    }
    // 懒加载三维数组
    lazy var emotionsArray: [[[HREmotion]]] = {
        return [
            self.seperateArray(self.defaultEmotions),
            self.seperateArray(self.EmojiEmotions),
            self.seperateArray(self.LXHEmotions),
        
        ]
 
        }()
    //  懒加载bundle文件
    private lazy var bundle: NSBundle = {
        //  获取bundle的路径,并根据路径加载bundle
        let path = NSBundle.mainBundle().pathForResource("Emoticons", ofType: "bundle")!
        let bundle = NSBundle(path: path)!
        return bundle
    }()
    //  加载默认表情
    private lazy var defaultEmotions:[HREmotion] = {
        return self.loadEmotionWithPath("default/info.plist")
    }()
    
    //  加载emoji表情
    private lazy var EmojiEmotions:[HREmotion] = {
        return self.loadEmotionWithPath("emoji/info.plist")
    }()
    
    //  加载浪小花表情
    private lazy var LXHEmotions:[HREmotion] = {
        return self.loadEmotionWithPath("lxh/info.plist")
    }()
    
     // MARK: - 拆分表情成二维数组
    private func seperateArray(array: [HREmotion]) -> [[HREmotion]]{
        let pages = (array.count - 1) / 20 + 1
        var tempArr = [[HREmotion]]()
        for i in 0..<pages {
            let loc = i * 20
            var len = 20
            //  如果有没有排满的页面
            if loc + len > array.count {
                len = array.count - loc
            }
            //  将数组按照一定的长度拆分
            let subArr = (array as NSArray).subarrayWithRange(NSMakeRange(loc, len)) as! [HREmotion]
            //  拆分的数组添加到二维数组中
            tempArr.append(subArr)
        }
        return tempArr
    }
    // MARK: - 根据绝对路径加载表情
    private func loadEmotionWithPath(subPath: String) -> [HREmotion]{
        let path = bundle.pathForResource(subPath, ofType: nil)!
       let arr = NSArray(contentsOfFile: path)
        var tempArr = [HREmotion]()
        let commmonPath = (path as NSString).stringByDeletingLastPathComponent
        
        for dict in arr! {
           let emotion = HREmotion(dict: dict as! [String : AnyObject])
            //  如果是图片需要指定图片的绝对路径
            if emotion.type! == "0" {
                //  拼接PNG的绝对路径
               emotion.absolutePathForPNG = commmonPath + "/" + emotion.png!
            }
            tempArr.append(emotion)
        }
        return tempArr
    }
    
     // MARK: - 根据chs匹配表情模型
    func getEmotionFromChs(chs: String) -> HREmotion? {
          //-----------------------------方法四
       if let emotion = defaultEmotions.filter({$0.chs == chs}).first{
            return emotion
        }
        if let emotion2 = LXHEmotions.filter({$0.chs == chs}).first {
            return emotion2
        }
          //-----------------------------方法三
//        let emotion = defaultEmotions.filter { (emotion) -> Bool in
//            emotion.chs == chs
//            }.first
//        let emotion2 = LXHEmotions.filter { (emotion) -> Bool in
//            emotion.chs == chs
//            
//            }.first
        
        
        //-----------------------------方法二
//       let emotion = defaultEmotions.filter { (emotion) -> Bool in
//            return emotion.chs == chs
//        }.first
//        let emotion2 = LXHEmotions.filter { (emotion) -> Bool in
//            return emotion.chs == chs
//            
//        }.first
//        if emotion != nil {
//            return emotion
//        }
//        if emotion2 != nil {
//            return emotion2
//        }
        //---------------------------方法1
        //  从默认表情中匹配
//        for value in defaultEmotions {
//            if chs == value.chs {
//                return value
//            }
//        }
        //  从浪小花中匹配
        
//        for value in LXHEmotions {
//            if chs == value.chs {
//                return value
//            }
//        }
        return nil
    }
     // MARK: -  根据模型生成富文本
    func generateAttributeStringWithEmotion(fontSize: CGFloat, emotion: HREmotion) -> NSAttributedString {
        //  根据表情模型的图片创建文本附件
       let textAttachment = HRTextAttachment()
        textAttachment.image = UIImage(named:  emotion.absolutePathForPNG!)
        textAttachment.emotion = emotion
        //  根据文字的行高,设置文本附件的大小
        let fontHeight = UIFont.systemFontOfSize(HomePageFontSize).lineHeight
        textAttachment.bounds = CGRect(x: 0, y: -4, width: fontHeight, height: fontHeight)
        //  根据文本附件生成富文本∫
        let attributeString = NSAttributedString(attachment: textAttachment)

        return attributeString
        
        
        
        
    }
}
